package com.cx.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cx.entity.Comment;
import com.cx.service.ArticleService;
import com.cx.service.CommentService;
import com.cx.service.StatisticService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private StatisticService statisticService;

    @GetMapping("/article/{id}")
    public String toArticleDetail(@PathVariable("id") Integer id,
                                   @RequestParam(required = false,defaultValue = "1") Integer cp,
                                   Model model) {
        //修改点击量
        statisticService.updateByArticleId(id);

        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", id).orderByDesc("created");
        PageHelper.startPage(cp, 5);
        PageInfo<Comment> pageInfo = new PageInfo<>(commentService.list(queryWrapper));
        model.addAttribute("article", articleService.getById(id));
        model.addAttribute("comments", pageInfo);
        return "client/articleDetails";
    }
}
